package com.spic.business.train.createtest.controller;

import com.spic.business.train.createtest.domain.TrainExam;
import com.spic.business.train.createtest.service.ITrainExamService;
import com.spic.business.train.trainInfo.dto.TrainInfoVo;
import com.spic.business.train.trainInfo.mapper.TrainInfoMapper;
import com.spic.business.train.trainUser.domain.TrainUser;
import com.spic.business.train.trainUser.mapper.TrainUserMapper;
import com.spic.business.xfalarm.DPage;
import com.spic.common.core.constant.HttpStatus;
import com.spic.common.core.utils.ServletUtils;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 创建考试Controller
 *
 * @author qishaoping
 * @date 2021-10-21
 */
@RestController
@RequestMapping("/train/createtest")
public class TrainExamController extends BaseController
{
    @Autowired
    private ITrainExamService trainExamService;
    @Autowired
    private TrainInfoMapper trainInfoMapper;
    @Autowired
    private TrainUserMapper trainUserMapper;

    /**
     * 查询创建考试列表
     */
//    @PreAuthorize(hasPermi = "business/train:createtest:list")
    @GetMapping("/list")
    public TableDataInfo list(TrainExam trainExam)
    {
        Integer pageNum = ServletUtils.getParameterToInt("pageNum");
        Integer pageSize = ServletUtils.getParameterToInt("pageSize");

        List<TrainExam> list = trainExamService.selectTrainExamList(trainExam);
        List<TrainExam> listTimp = new ArrayList<>();

        for (TrainExam exam : list)
        {

            TrainUser tUser = new TrainUser();
            tUser.setTrainInfoId(exam.getTrainId());
            List<TrainUser> uList = trainUserMapper.selectTrainUserList(tUser);
            for (TrainUser trainUser : uList)
            {
                TrainExam e = new TrainExam();
                BeanUtils.copyProperties(exam,e);
                Long userId = trainUser.getUserId();
                e.setUserId(userId);
                listTimp.add(e);
            }
        }
        DPage<TrainExam> page = new DPage<>(listTimp,pageNum,pageSize);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getList());
        rspData.setTotal(page.getTotal());
        return rspData;
    }

    @GetMapping("/listbyUser")
    public TableDataInfo listbyUser(TrainExam trainExam)
    {
        Integer pageNum = ServletUtils.getParameterToInt("pageNum");
        Integer pageSize = ServletUtils.getParameterToInt("pageSize");
        List<TrainExam> list = trainExamService.selectTrainExamListByUser(trainExam);
        DPage<TrainExam> page = new DPage<>(list,pageNum,pageSize);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getList());
        rspData.setTotal(page.getTotal());
        return rspData;

    }
    @GetMapping("/trainId")
    public AjaxResult trainId(){
      List<TrainInfoVo> trainInfos=  trainInfoMapper.selectTrainInfoListNotInexam();
      return AjaxResult.success(trainInfos);
    }
    /**
     * 导出创建考试列表
     */
    @PreAuthorize(hasPermi = "business/train:createtest:export")
    @Log(title = "创建考试", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainExam trainExam) throws IOException
    {
        List<TrainExam> list = trainExamService.selectTrainExamList(trainExam);
        ExcelUtil<TrainExam> util = new ExcelUtil<TrainExam>(TrainExam.class);
        util.exportExcel(response, list, "创建考试数据");
    }

    /**
     * 获取创建考试详细信息
     */
//    @PreAuthorize(hasPermi = "business/train:createtest:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(trainExamService.selectTrainExamById(id));
    }

    /**
     * 新增创建考试
     */
//    @PreAuthorize(hasPermi = "business/train:createtest:add")
    @Log(title = "创建考试", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrainExam trainExam)
    {
        return AjaxResult.success(trainExamService.insertTrainExam(trainExam));
    }

    /**
     * 修改创建考试
     */
//    @PreAuthorize(hasPermi = "business/train:createtest:edit")
    @Log(title = "创建考试", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainExam trainExam)
    {
        return toAjax(trainExamService.updateTrainExam(trainExam));
    }

    /**
     *
     * 考试结束更新表数据参数 和培训信息
     * @param trainExam
     * @return
     */
    @Log(title = "修改考试", businessType = BusinessType.UPDATE)
    @PutMapping("/updateExamandTrainInfo")
    public AjaxResult updateExamandTrainInfo(@RequestBody TrainExam trainExam)
    {
        return toAjax(trainExamService.updateTrainExamAndTrainInfo(trainExam));
    }

    /**
     * 删除创建考试
     */
//    @PreAuthorize(hasPermi = "business/train:createtest:remove")
    @Log(title = "创建考试", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(trainExamService.deleteTrainExamByIds(ids));
    }



    /**
     * 查询创建考试列表
     */
//    @PreAuthorize(hasPermi = "business/train:createtest:list")
    @GetMapping("/trainExamList")
    public TableDataInfo trainExamList(TrainExam trainExam)
    {
        Integer pageNum = ServletUtils.getParameterToInt("pageNum");
        Integer pageSize = ServletUtils.getParameterToInt("pageSize");

        List<TrainExam> list = trainExamService.selectTrainExamList(trainExam);



        DPage<TrainExam> page = new DPage<>(list,pageNum,pageSize);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getList());
        rspData.setTotal(page.getTotal());
        return rspData;
    }
}
